#! /bin/bash

# create data directory
if [ ! -d $HOME/data/south_africa ]; then
  mkdir -p $HOME/data/south_africa
fi

# location of online data
DATAPATH="http://www.nwu.ac.za/sites/www.nwu.ac.za/files/files/p-nm/static-content"

# pick filename and station
DATA="Tsumeb_1976_339to2014_31.dat"
station="tsmb"

#DATA="Hermanus_1957_149to2014_31.dat"
#station="hrms"

#DATA="Sanae_NM64__1964_233_to_2014_31.dat"
#station="snae"

# use symlinks if it is already available on the filesystem
cd $HOME/data/south_africa/
# download file only if it is not present yet
if [ ! -e $DATA ]; then
  wget $DATAPATH/$DATA
fi
ln -s $DATA tsmb_1h.csv

# split data by year

#type="ori"
type="1h"

ymin=1976
ymax=2014

mkdir -p ${station}/${type}

# split CVS file by year
for year in `seq ${ymin} ${ymax}`;
  do
    echo $year
    grep ^$year ${station}_1h.csv > ${station}/${type}/${station}_${year}_${type}.csv
done

